package com.qfedu.food.controller;

import com.qfedu.food.pojo.TFood;
import com.qfedu.food.service.intf.CollectService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping
@Api(tags = "实现菜品的收藏功能与已收藏菜品的查询")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     *
     * @param username 用户名
     * @param fname 美食名称
     *        uid   通过用户名查询到的用户id
     *        fid   通过美食名查询到的美食id
     */
    @PostMapping("/Collect/add.do")
    public void addCollect(String username,String fname) {
        if(username.length()>0 && fname.length()>0) {
            Integer uid = collectService.getUidByUsername(username);
            Integer fid = collectService.getFidByFname(fname);
            if (uid != null && uid != 0) {
                Integer collect = collectService.getCollect(uid, fid);
                if (collect == 1) {
                    collectService.delCollectByUidAndFid(uid);
                } else if (collect == 0) {
                    collectService.updateCollectByUidAndFid(uid);
                } else if (collect == 2) {
                    collectService.addCollect(uid, fid);
                }
            }else {
                System.out.println("请输入正确的用户名");
            }
        } else {
            System.err.println("请输入用户名");
        }
    }

    @PostMapping("/Collect/all.do")
    public void getAllCollect(String username){
        if (username.length()>0) {
            Integer uid = collectService.getUidByUsername(username);
            if (uid != null && uid != 0) {
                List<Integer> fids = collectService.getFidByUid(uid);
                if (fids.size()>0) {
                    List<TFood> food = collectService.getFoodByid(fids);
                    System.out.println(food);
                }
            }else {
                System.out.println("请输入正确的用户名");
            }
        }else {
            System.out.println("请输入用户名");
        }
    }
}
